package com.ruoyi.crm.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.mapper.ClueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.AssignRecordMapper;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.service.IAssignRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 线索分配记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-16
 */
@Service
public class AssignRecordServiceImpl implements IAssignRecordService {
    @Resource
    private AssignRecordMapper assignRecordMapper;
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private BusinessMapper businessMapper;

    /**
     * 查询线索分配记录
     *
     * @param id 线索分配记录主键
     * @return 线索分配记录
     */
    @Override
    public AssignRecord selectAssignRecordById(Long id) {
        return assignRecordMapper.selectAssignRecordById(id);
    }

    /**
     * 查询线索分配记录列表
     *
     * @param assignRecord 线索分配记录
     * @return 线索分配记录
     */
    @Override
    public List<AssignRecord> selectAssignRecordList(AssignRecord assignRecord) {
        return assignRecordMapper.selectAssignRecordList(assignRecord);
    }

    /**
     * 新增线索分配记录
     * 1、更新线索或商机的状态为已分配
     * 2、更新分配记录表对应Id的latest值为0
     * 3、插入分配表，latest值为1
     *
     * @param assignRecord 线索分配记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssignRecord(AssignRecord assignRecord) {
        AssignRecord.Type type = AssignRecord.Type.valueOf(assignRecord.getType());
        if (type == AssignRecord.Type.CLUE) {
            Clue clue = clueMapper.selectClueById(assignRecord.getAssignId());
            clue.setStatus(Clue.Status.ASSIGNED.getValue());
            clue.setUpdateBy(SecurityUtils.getUsername());
            clue.setUpdateTime(new Date());
            clue.setFalseCount(0L);
            clueMapper.updateClue(clue);
        } else if (type == AssignRecord.Type.BUSINESS) {
            Business business=businessMapper.selectBusinessById(assignRecord.getAssignId());
            business.setStatus(Clue.Status.ASSIGNED.getValue());
            business.setLastUpdateTime(DateUtils.getNowDate());
            business.setUpdateBy(SecurityUtils.getUsername());
            businessMapper.updateBusiness(business);
        } else {
            throw new IllegalArgumentException("枚举类型不存在");
        }
        //2.更新分配记录表对应Id的latest值为0
        assignRecordMapper.updateLatest(assignRecord.getAssignId(), type.getValue(), "0");
        //3.插入分配记录，latest值为1;
        assignRecord.setLatest("1");
        assignRecord.setType(type.getValue());
        assignRecord.setCreateTime(DateUtils.getNowDate());
        return assignRecordMapper.insertAssignRecord(assignRecord);
    }

    /**
     * 修改线索分配记录
     *
     * @param assignRecord 线索分配记录
     * @return 结果
     */
    @Override
    public int updateAssignRecord(AssignRecord assignRecord) {
        return assignRecordMapper.updateAssignRecord(assignRecord);
    }

    /**
     * 批量删除线索分配记录
     *
     * @param ids 需要删除的线索分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordByIds(Long[] ids) {
        return assignRecordMapper.deleteAssignRecordByIds(ids);
    }

    /**
     * 删除线索分配记录信息
     *
     * @param id 线索分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordById(Long id) {
        return assignRecordMapper.deleteAssignRecordById(id);
    }

    @Override
    public int insertAssignRecords(List<AssignRecord> assignRecords) {
        int count = 0;
        for (AssignRecord assignRecord : assignRecords) {
            count += this.insertAssignRecord(assignRecord);
        }
        return count;
    }

    @Override
    public void updateAssignRecord(Long id, String type, String latest) {
        assignRecordMapper.updateLatest(id,type,latest);
    }


}
